package com.bdqn.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.entity.PmsProductCategory;
import com.bdqn.entity.PmsSpu;
import com.bdqn.entity.allParams.PmsSpuParam;
import com.bdqn.entity.responseDate.pmsProduct.*;
import com.bdqn.mapper.PmsProductCategoryMapper;
import com.bdqn.mapper.PmsSkuMapper;
import com.bdqn.mapper.PmsSpuMapper;
import com.bdqn.service.PmsSpuService;
import com.bdqn.util.PageDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 商品spu表 服务实现类
 * </p>
 *
 * @author hunter
 * @since 2022-04-30
 */
@Service
public class PmsSpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> implements PmsSpuService {
    @Autowired
    private PmsSpuMapper pmsSpuMapper;
    @Autowired
    private PmsSkuMapper pmsSkuMapper;
    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;

    //这里查询的是不分页的商品展示列表
    @Override
    public List<PmsSpu> searchPmsSpu(PmsSpuParam pmsSpuParam) {
        return pmsSpuMapper.searchPmsSpu(pmsSpuParam);
    }

    //这里查询出来的是分页对象
    @Override
    public PageDto<PmsSpu> spuPage(PmsSpuParam pmsSpuParam) {
        //进行分页操作
        PageHelper.startPage(pmsSpuParam.getPageNo(), pmsSpuParam.getPageSize());
        PageInfo pageInfo = new PageInfo(pmsSpuMapper.searchPmsSpu(pmsSpuParam));
        //封装PageDto，与前端对接
        PageDto pageDto = new PageDto();
        pageDto.setPageNo(pageInfo.getPageNum());
        pageDto.setPageSize(pageInfo.getPageSize());
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setPages(pageInfo.getPages());
        pageDto.setBeginPos(pageInfo.getPageNum());
        pageDto.setList(pageInfo.getList());
        return pageDto;
    }

    //通过商品id 获取spu和sku信息
    @Override
    public PmsProductDetailDto getPmsSpuDtoById(Long id) {
        //通过id 获取spu和详情图片
        PmsSpuDto spu = pmsSpuMapper.getPmsSpuDtoById(id);

        //通过id 获取spu和sku及sku_pic 信息
        List<PmsSkuDto> pmsSkuDtoBySpuId = pmsSkuMapper.getPmsSkuDtoBySpuId(id);

        // 通过商品分类id数组 获取商品类别信息，封装成map,只存储 商品类别id和name
        List<Map<String, Object>> pmsCategoryMapList = new ArrayList<>();
        //创建集合的时候 初始化时候进行赋值操作 一级分类id和二级分类id
        List<Long> ids = Arrays.asList(spu.getCid1(), spu.getCid2());
        //通过一级分类id和二级分类id 获取对应的分类条数信息
        List<PmsProductCategory> categoryList = pmsProductCategoryMapper.selectBatchIds(ids);

        for (int i = 0; i < categoryList.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", categoryList.get(i).getId());
            map.put("name", categoryList.get(i).getName());
            pmsCategoryMapList.add(map);
        }
//          拼接spu和sku信息
        PmsProductDetailDto pmsProductDetailDto = new PmsProductDetailDto();
//        赋值进去
        pmsProductDetailDto.setSpu(spu);
        pmsProductDetailDto.setSkuList(pmsSkuDtoBySpuId);
        pmsProductDetailDto.setCategories(pmsCategoryMapList);
        return pmsProductDetailDto;
    }
}
